//index.js
//获取应用实例
const Page = require('../../utils/ald-stat.js').Page;
const http=require('../../api/http.js');
const app = getApp();
Page({
  data: {
    imgUrls:[],   //轮播图
    brandList:[],  //品牌列表
    // imgUrls: [
    //   '../../resource/home/banner01.png',     'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
    //   'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    // ],
    indicatorDots: true,    //是否出现原点
    autoplay: true,         //是否自动轮播
    circular:true,          //是否衔接轮播
    interval: 5000,         //自动切换时间间隔
    duration: 1000 ,        //滑动动画时长
    current:0,
    showMore:false,            //是否还有更多数据
    userIcon:'',            //用户头像
    page:1,
    next:''
  },
 
  onLoad:function(options){
    this.http_swipe();
    
  },
  onShow:function(){
    let _this=this;
    this.http_brandList();
    if (wx.getStorageSync('userIcon')){
      let userIcon = wx.getStorageSync('userIcon');
      _this.setData({
        userIcon: userIcon
      }) 
    }   
  },
  onReady(){
    
  },
  //请求轮播图接口
  http_swipe(){
    let page = 1;
    http.getSwipe(page,res =>{
      this.setData({
        imgUrls:res.result,
        page:1
      })
    },err =>{

    });
  },
  //请求品牌列表
  http_brandList(){
    http.getBrandList(this.data.page, res => {
      this.setData({
       brandList:res.result,
       next: res.next
      })
      if (res.result.length < res.count){
        this.setData({
          showMore: true, 
        })
      }
    }, err => {

    });
  },
  // 点击申请入驻
  applyEnter() {
    let _this=this;
    wx.navigateTo({    //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
      url: "/pages/enter/enter",
      success: function () {
        _this.setData({
          page: 1
        })
      },
      fail: function () {
      }
    })
  },
  // 点击banner图进入知了指数介绍页
  // topCicadas(){
  //   let _this = this;  
  //   wx.navigateTo({
  //     url: '/pages/cicadasIndex/cicadasIndex',
  //     success(){
  //       _this.setData({
  //         page: 1
  //       })
  //     }
  //   })
  // },
  //点击轮播图触发事件
  swipclick: function (e) {//点击图片触发事件
   let _this=this;
    wx.navigateTo({
      url: '/pages/guiding/guiding?' + 'url=' + _this.data.imgUrls[this.data.current].url,
    })
  },
  bindchange: function (e) {//轮播图发生改变
    this.setData({
      current: e.detail.current
    })
  },
  //点击品牌列表进入品牌详情
  toBrandDetail(e){
    let _this=this;
    let url = e.currentTarget.dataset.item.url;
    let id=url.split('/')[url.split('/').length-2];
    wx.navigateTo({    //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
      url: "/pages/indexDetail/indexDetail?id=" + id + '&home=1',
      success:function(){
       _this.setData({
         page:1
       })
      },
      fail:function(){
      }
    })
  },
  //上拉加载更多
  onReachBottom(){
    var _this = this;   
    if (_this.data.next !=null) {
      _this.setData({
        page: _this.data.page + 1
      })
      http.getBrandList(_this.data.page, res => {
        _this.setData({
          brandList: _this.data.brandList.concat(res.result),
          next:res.next
        })
        if (_this.data.brandList.length >= res.count) {
          _this.setData({
            showMore: false,
          })
        }
      }, err => {
      console.log(err);
      });
    }
  },
  //分享给朋友
  onShareAppMessage(res) {
    let _this = this;
    return {
      title: '知了指数',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功之后的回调 　　　
        if (res.errMsg == 'shareAppMessage:ok') {
          wx.showToast({
            title: '分享成功',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {

      },
      complete: function (res) {

      }
    }
  }

})
